import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { WeaDialog,WeaLocaleProvider } from "ecCom";
import { Button,Modal,message } from "antd";
import Workflow from '../sign/Workflow.js';
import * as api from "../../api/kqRemain.js";
import '../../style/kqRemain.less';
const getLabel = WeaLocaleProvider.getLabel;
const {info, confirm} = Modal;

export default class KqRemain extends Component {
	constructor(props){
        super(props);
        this.state = {
            msg:'',
            visible:false,
            canSignOnPC:false,
            wfDialogVisible: false,
        }
    }

	componentDidMount() {
		api.getRemindInfo().then(datas => {
			const {
				isOpen
			} = datas;

			['msg', 'canSignOnPC'].forEach(val => {
				if (datas[val]) {
					this.setState({
						[val]: datas[val]
					})
				}
			})

			if (isOpen) {
				this.openDialog();
			}
		})
	}

	getTimeLineDatas = () => {
		return new Promise( (resolve , reject) => {
	        api.getButtons().then(data => {
	            if(data.status === '1'){
	                const {timeline} = data;

	                this.signParams = timeline.find(data =>(data.active === '1' && data.type === 'on') )
	            	resolve()
	            }else{
	                message.error(data.message);
	                reject();
	            }
	        })
		})
    }

	sign = () => {
		api.punchButton(this.signParams).then(data => {
			if (data.message && data.message.trim().length > 0) {
				if (data.success === '1') {
					message.success(data.message);
				} else {
					message.info(data.message);
				}

				// info({
				// 	content: <div dangerouslySetInnerHTML={{__html: data.message}}/>,
				// 	title: getLabel(131329, '信息确认'),
				// 	okText: getLabel(33703, '确定')
				// });
			} else if (['1', '2'].indexOf(data.kqstatus) > -1) {
				message.warning(getLabel('512595','	您的考勤数据异常，如有需要请提交考勤流程'));
								
				// confirm({
				// 	content: <div><div>{`${getLabel('18949','打卡时间')}: ${data.signdate} ${data.signtime}`}</div><div>{getLabel('500500','您的考勤数据异常，是否要提交考勤流程？')}</div></div>,
				// 	title: getLabel(131329, '信息确认'),
				// 	okText: getLabel(33703, '提交考勤流程'),
				// 	cancelText: getLabel(32694, '不需要'),
				// 	onOk: () => this.showWorkflow(),
				// });
			}

			
		})
	}


    sure = () => {
    	const {canSignOnPC} = this.state;

    	this.closeDialog();

    	if(canSignOnPC){
    		this.getTimeLineDatas().then(() => {
    			this.sign()
    		})
    	}
    }

    openDialog = () => {
    	this.setState({ visible: true })
    }

    closeDialog = () => {
    	this.setState({ visible: false })
    }

    wfDialogCancelHandle = () => {
    	this.setState({wfDialogVisible: false})
    }

    showWorkflow = () => {
       this.setState({wfDialogVisible: true})
    }

    getButtons=()=>{
    	const {canSignOnPC} = this.state;

    	const btns = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pla18y`} type='primary' onClick={this.sure}>{getLabel(826,'确定')}</Button>,
		]

		if (canSignOnPC) {
			btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@l0yzh1`} onClick={this.closeDialog}>{getLabel(201,'取消')}</Button>);
		}

		return btns;
    }

	render() {
		const {visible,wfDialogVisible,msg}= this.state;

		return (
			<div>
				<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@s17ukz`}
					icon={'icon-coms02-Warning'}
					iconBgcolor={'#ff9900'}
					visible={visible}
					title={getLabel(386786,'信息确认')}
					style={{ width: 400, height: 120 }}
					onCancel={this.closeDialog}
					buttons={this.getButtons()}
				>
					<div className='kqRemain'>{msg}</div>
				</WeaDialog>
				{
					wfDialogVisible&& (
						<Workflow ecId={`${this && this.props && this.props.ecId || ''}_Workflow@w7s1fb`} wfDialogVisible={wfDialogVisible} wfDialogCancelHandle={this.wfDialogCancelHandle}/>
					)
				}
			</div>
		);
	}
}

(function() {
    const div = document.createElement("div");
    document.body.appendChild(div);
    ReactDOM.render(<KqRemain ecId={`${this && this.props && this.props.ecId || ''}_KqRemain@j1i1qu`} />, div);
})();